
local imgs = {
    [[ui\英雄选择\蓝.tga]],
    [[ui\英雄选择\紫.tga]],
    [[ui\英雄选择\黄.tga]],
}
local names = {
    [[ui\英雄选择\蓝-1.tga]],
    [[ui\英雄选择\紫-1.tga]],
    [[ui\英雄选择\黄-1.tga]],
}
local sx = {
    ['攻击'] = [[ui\英雄选择\攻击.tga]],
    ['力量'] = [[ui\英雄选择\力量.tga]],
    ['敏捷'] = [[ui\英雄选择\敏捷.tga]],
    ['智力'] = [[ui\英雄选择\智力.tga]],
    ['通用'] = [[ui\英雄选择\通用.tga]],
}

--选择英雄
local empty = {}
class.herospSelect = extends(class.button) {
    reload = true,

    new = function (parent,x,y,w,h)
        local panel = class.herospSelect:builder 
        {
            parent = parent,
            x = x,
            y = y,
            w = w,
            h = h,
        }
        return panel
    end,
    init = function (self)
        class.button.init(self)
        self:set_normal_image([[textures\black32.blp]])
        self:set_normal_image(imgs[1])
        self.highlight = self:add_panel([[UI\选中框.tga]],-10,-0,self.w+20,self.h+10)
        self.highlight:hide()
        
        self.model = self:add_actor('英雄2',10,10,self.w-20,self.h)
        self.tag = self:add_panel(sx['力量'],self.w-73,26,57,57)
        self.title_p = self:add_panel(names[1],(self.w-284)/2,self.h - 90,284,55)
        self.title = class.text:builder {
            parent = self,
            y = self.h - 80,
            h = 30,
            text = '英雄2',
            shadow_offset_x = 2,
            shadow_offset_y = -2,
            shadow_color = 0xff000000,
            --color = 0xffffff00,
            outline = true,
            font_size = 20,
            align = 5,
        }
        -- self.sp1 = self:add_button([[ReplaceableTextures\CommandButtons\BTNWallOfFire.blp]],70,self.h - 84,64,64)
        -- self.sp2 = self:add_button([[ReplaceableTextures\CommandButtons\BTNWallOfFire.blp]],self.w-64-70,self.h - 84,64,64)
        
        -- self.sp1:set_highlight([[UI\Widgets\BattleNet\bnet-button01-highlight-mouse.blp]])
        -- self.sp2:set_highlight([[UI\Widgets\BattleNet\bnet-button01-highlight-mouse.blp]])

        return self
    end,

    set_data = function(self,temp)
        local pz = temp['品质']
        local sx_class = temp['分属']
        local ski1 = ac.lni('skill',temp['天赋']) or empty
        local ski2 = ac.lni('skill',temp['技能']) or empty
        self.skill = ski2
        self:set_normal_image(imgs[pz])
        self.title_p:set_normal_image(names[pz])
        self.tag:set_normal_image(sx[sx_class])
        self.model:set_actor(temp.name)
        self.model:set_size(1.3)
        self.title:set_text(temp.title or temp.name)
        -- self.sp1:set_normal_image(ski1.art)
        -- self.sp2:set_normal_image(ski2.art)
        -- self.sp1.skill = ski1
        -- self.sp2.skill = ski2
        
        --self.jb:set_text(('|cffffff00%s|r'):format(skill:get_tooltips('jb_name')))
    end,
    on_button_mouse_enter = function(self)
        if self.skill then
            local skill = self.skill
            local tip = format_obj(skill,skill.tip)
            local art = skill.art
            local resource = {cool = skill.cool}
            self:tooltip(skill.title or skill.name,tip,nil,nil,resource,art)
        end
    end
}


local ui = class.select_panel.create('玩家-UI选择英雄')
ui:set_select_button(3,class.herospSelect,322,461)
ui:hide()

--重置按钮
local refresh = ui:add_namebutton('刷新',(1920-300)/2,680,150,60,12,5)
ac.hero_refresh = refresh

function refresh:on_button_clicked()
    ac.sync('同步-刷新选择英雄')
end


--关闭
local esc = ui:add_namebutton('待会再选',(1920-300)/2 + 180,680,150,60,12,5)

function esc:on_button_clicked()
    ui:hide()
end

function ui:update()
    local player = ac.player.self
    local hero = player.hero
    if hero then
        local num = hero:get('英雄刷新次数')
        local pay = hero:get_data('付费英雄刷新',0)
        if num>0 then
            refresh.text:set_text(('免费刷新(|cff00ff00%0.f|r)'):format(num))
        else
            refresh.text:set_text(('刷新(|cffff0000%0.f|r杀敌)'):format(pay))
        end
    end
end

ac.sync:event '玩家-UI选择英雄'(function(_,player,index)
    local info = player:get_data('选择英雄信息')
    local temp = info and info[1]
    local func = info and info[2]
    if temp and func then
        func(temp[index],index)
    end
end)


ac.game:event '玩家-选择英雄面板'(function(_,player,data,func)
    if #data==0 then
        log.error('选择英雄面板-选项为0')
        return
    end
    player:set_data('选择英雄信息',{data,func})
    if player:is_self() then
        ui:set_data(data,1)
        ui:show()
    end
end)

ac.game:event '玩家-显示选择英雄'(function(_,player)
    local info = player:get_data('选择英雄信息')
    if player:is_self() then
        ui:set_data(info[1],1)
        ui:show()
    end
end)
